package com.alks.function.service.impl.stock;

import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.excelUtils.ExcelPoiUtils;
import com.alks.entity.data.entity.*;
import com.alks.entity.data.enums.ResultCodeEnum;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.PcLastStockDto;
import com.alks.function.data.dto.sole.PcSoleSupportingDto;
import com.alks.function.data.request.last.PcLastStockRequest;
import com.alks.function.data.request.last.PcSoleStockMessageRequest;
import com.alks.function.data.request.last.SoleSupportingInformationRequest;
import com.alks.function.mapper.last.LastStockMapping;
import com.alks.function.service.PcLastStockService;
import com.alks.function.service.async.PcSoleAsyncService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author:xxxxx
 * @create: 2023-07-25 10:40
 * @Description: 底仓库存
 */
@Service
@Slf4j
public class PcLastStockServiceImpl implements PcLastStockService {
    @Autowired
    LastStockMapping lastStockMapping;
    @Override
    public ResponseInfo getLastStockCondition() {
        Map<String,List> map = new HashMap<>();
        //底模
        List<String> moldNos = lastStockMapping.getMoldNos();
        //部位名称
        List<String> parts = lastStockMapping.getLastParts();
        map.put("moldNos",moldNos);
        map.put("parts",parts);
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo listLastStock(PcLastStockRequest request) {
        if (request == null){
            return ResponseInfo.error(ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getMessage(), ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getCode());
        }
        if (request.getPageNum() == null || request.getPageSize() == null) {
            request.setPageNum(1);
            request.setPageSize(10);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize(),false);
        PcLastStockDto dto = new PcLastStockDto();
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<PcLastStockResp> list = lastStockMapping.listLastStock(request);
        Long total = lastStockMapping.getLastStockTotal(request);
        Long stockTotal = lastStockMapping.getStockTotal(request);
        dto.setList(list);
        dto.setTotal(total);
        dto.setStockTotal(stockTotal);
        return ResponseInfo.ok(dto);
    }

    @Override
    public ResponseInfo getPcLastStockSizeDetail(String lotNo, String partName) {
        String partType = null;
        switch (partName){
            case "大底":
                partType = "1";
                break;
            case "鞋垫":
                partName = "面衬";
                partType = "2";
                break;
        }
        HashMap<String, List> map = new HashMap<>();
        List<PcLastStockSizeResp> sizes = lastStockMapping.getPcLastStockSizeDetail(lotNo,partName);
        List<PcLastStockInOut> inDetail = lastStockMapping.getPcLastStockInDetail(lotNo,partType,"I");
        List<PcLastStockInOut> outDetail = lastStockMapping.getPcLastStockInDetail(lotNo,partType,"O");
        map.put("sizes",sizes);
        map.put("inDetail",inDetail);
        map.put("outDetail",outDetail);
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo getStockMessageInOut(PcSoleStockMessageRequest request) {
        if (request == null){
            return ResponseInfo.error(ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getMessage(), ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getCode());
        }
        if (request.getPageNum() == null || request.getPageSize() == null) {
            request.setPageNum(1);
            request.setPageSize(10);
        }
        request.setCompanyId(UserIdThread.get().getCompanyId());
        PageHelper.startPage(request.getPageNum(), request.getPageSize(),false);
        Map<String, Map> map = new HashMap<>();
        Map<String, Object> soleMessage = new HashMap<>();
        Map<String, List> outInStock = new HashMap<>();
        List<PcLastStockResp> list = lastStockMapping.getPcSoleMessage(request);
        List<Map<String,Double>> inStock = lastStockMapping.getPcSoleIn(request);
        List<Map<String,Double>> outStock = lastStockMapping.getPcSoleOut(request);
        Long total = lastStockMapping.getPcSoleTotal(request);
        Long stockTotal = lastStockMapping.getPcSoleStockTotal(request);
        soleMessage.put("list",list);
        soleMessage.put("total",total);
        soleMessage.put("stockTotal",stockTotal);
        outInStock.put("inStock",inStock);
        outInStock.put("outStock",outStock);
        map.put("soleMessage",soleMessage);
        map.put("outInStock",outInStock);
        return ResponseInfo.ok(map);
    }

    @Autowired
    PcSoleAsyncService pcSoleAsyncService;
    //异步查询 面底配套
    @Override
    public ResponseInfo getSoleSupporting(SoleSupportingInformationRequest request) throws ExecutionException, InterruptedException {
        if (request == null){
            return ResponseInfo.error(ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getMessage(), ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getCode());
        }
        if (request.getPageNum() == null || request.getPageSize() == null) {
            request.setPageNum(1);
            request.setPageSize(10);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize(),false);
        PcSoleSupportingDto dto = new PcSoleSupportingDto();
        List<PcSoleSupportingResp> pcSoleSupportingList = lastStockMapping.getPcSoleSupportingList(request);
        Future<Long> total = pcSoleAsyncService.getPcSoleSupportingTotal(request);
        Future<List<Map<String, BigDecimal>>> sum = pcSoleAsyncService.getPcSupportingSum(request);
        dto.setTotal(total.get());
        dto.setSums(sum.get());
        dto.setList(pcSoleSupportingList);
        return ResponseInfo.ok(dto);
    }

    //面底配料明细
    @Override
    public ResponseInfo getSoleSupportingDetail(String lotNo) {
        Map<String, Object> map = new HashMap<>();
        List<PcSoleSupportingDetailResp> list = lastStockMapping.getSoleSupportingDetail(lotNo);
        BigDecimal stitchSum = new BigDecimal(0);
        BigDecimal soleSum = new BigDecimal(0);
        BigDecimal insoleSum = new BigDecimal(0);
        BigDecimal matchSum = new BigDecimal(0);
        for (PcSoleSupportingDetailResp sole : list) {
            BigDecimal soleStock = sole.getSoleStock();
            BigDecimal stitchStock = sole.getStitchStock();
            if (soleStock != null && stitchStock != null){
                BigDecimal min = soleStock.min(stitchStock);
                sole.setMatchQty(min);
                soleSum = soleSum.add(sole.getSoleStock());
                stitchSum = stitchSum.add(sole.getStitchStock());
                matchSum = matchSum.add(sole.getMatchQty());
            }
            if (sole.getInSoleStock() != null){
                insoleSum = insoleSum.add(sole.getInSoleStock());
            }
            sole.setMidSoleStock(new BigDecimal(0));
        }
        map.put("list",list);
        map.put("soleSum",soleSum);
        map.put("stitchSum",stitchSum);
        map.put("matchSum",matchSum);
        map.put("insoleSum",insoleSum);
        return ResponseInfo.ok(map);
    }

    @Override
    public void downloadStockInOut(HttpServletResponse response, PcSoleStockMessageRequest req) throws UnsupportedEncodingException {
        Map<String,Object> map = new HashMap<>();
        req.setCompanyId(UserIdThread.get().getCompanyId());
        List<Map<String,Object>> list = lastStockMapping.getStockInOut(req,"I");
        List<Map<String,Object>> outList = lastStockMapping.getStockInOut(req,"O");
        list.addAll(outList);
        map.put("list",list);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        map.put("inOutStartDate",format.format(req.getInOutStartDate()));
        map.put("inOutEndDate",format.format(req.getInOutEndDate()));
        BigDecimal inTotal = new BigDecimal(0);
        BigDecimal outTotal = new BigDecimal(0);
        for (Map<String, Object> stringObjectMap : list) {
            Long in = (Long) stringObjectMap.get("inQty");
            Long out = (Long) stringObjectMap.get("outQty");
            if (in == null){
                in = (long)0;
            }
            if (out == null){
                out = (long)0;
            }
            inTotal = inTotal.add(new BigDecimal(in));
            outTotal = outTotal.add(new BigDecimal(out));
        }
        map.put("inTotal",inTotal);
        map.put("outTotal",outTotal);
        map.put("name",req.getPartName());
        ExcelPoiUtils.writeExcelList(response,map,"templates/大底收发报表模板.xls");
    }
}
